package com.itheima;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/ServletAdd")
public class FilterAdd implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Student student =(Student) request.getSession().getAttribute("stu");
        if (request.getRequestURI().endsWith("/ServletAdd")){
            filterChain.doFilter(request,response);
            return;
        }
        if (student.getId()!=null && !"".equals(student.getId())){
            filterChain.doFilter(request,response);
            return;
        }else{
            response.getWriter().write("请输入正确的学生信息...");
            response.setHeader("Refresh","2;UTF=add.jsp");
            return;
        }
    }
}
